package com.ruoyi.system.controller;

import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.encry.AESUtils;
import com.ruoyi.common.utils.encry.EncryPtionUtils;
import com.ruoyi.common.utils.encry.RSAUtils;
import com.ruoyi.system.domain.vo.DecryptionBody;
import org.springframework.web.bind.annotation.*;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
import java.util.HashMap;

@RestController
@RequestMapping("/encryption")
public class EncryPtionTextnterface {

    /**
     * 这个接口专门给apipost等接口测试工具， 使用固定的key和iv进行接口测试用。 返回使用固定key和iv，加密后的密文
     * 在实际项目中， 需要每次随机生成key和iv。
     * 使用此接口加密的body必须设置如下key和iv才能正常解密.
     * ---
     * - 参数名：X-Encrypted-AES-Key
     * - 参数值：XP861wX1Y7QnKuikJjm/FCbMfk2JD5VVOpxOdHDSAah0UCOi1bvGFZ8dC88dWcB/+JORl7g8VzRsyquhXK9rpZuhiXFfmxcYjCIk75gS0iw/AXAeB2vM1yRM8LUNnoV/FAN0R8ailCMIKy+6JWx/Sho5mbDrn4aWJmltnhAqvzMLqHSn3ZYC0/wdUj3itu3WWrxKQn+ckwD1aTMoB4ymNs/NNBkbMmwaSjMRzcYsTxEhoTB92DVzkcOYvePuWbGOx9z+Fhe6nEk8EIqnfkSvH3KOxqxYTnJUUH2FXs92VYT7qiJBnXwP3Il4qYL05w0HfedYgN7IuJqgVAKNe7GSJg==
     * ---
     * - 参数名：X-Encrypted-AES-IV
     * - 参数值：YWJjZGVmZ2hpamts
     * @param content
     * @return
     */
    @PostMapping("/getEncryptionBody")
    public AjaxResult getEncryptionBody(@RequestBody String content){
        try {
            KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
            aesKeyGen.init(256);
            SecretKey aesKey = aesKeyGen.generateKey();
            byte[] key = aesKey.getEncoded();

            byte[] iv = AESUtils.generateIvTag(12);
            String body = EncryPtionUtils.getBodyForApiPost(content, key, iv);
            HashMap<String, String> result = new HashMap<>();
            result.put("key", Base64.getEncoder().encodeToString(key));
            result.put("iv", Base64.getEncoder().encodeToString(iv));
            result.put("body", body);
            String aesKeyRSA = RSAUtils.rsaEncryPtion(aesKey);
            result.put("esaKeyRSA", aesKeyRSA);
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error(-1, "加密失败");
        }
    }

    @PostMapping("/getDecryptionBody")
    // apipost  无法发送json格式， 只能接收String然后自己转换实体类了。
    public AjaxResult getDecryptionBody(@RequestBody String bodyStr){
        ObjectMapper objectMapper = new ObjectMapper();
        DecryptionBody body = null;
        try {
            body = objectMapper.readValue(bodyStr, DecryptionBody.class);
            return AjaxResult.success(EncryPtionUtils.getDecodeBodyForApiPost(body.getData(), body.getKey(), body.getIv()));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(-1, "加密失败");
        }
    }

    @PostMapping("/test")
    public AjaxResult test(@RequestBody String content){
        return AjaxResult.success(content);
    }
}
